//
//  CYMyNoticeCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/3.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYMyNoticeCell.h"
@interface CYMyNoticeCell(){
    CGFloat offset_width;//
    CGFloat offset_height;//
}
@property (nonatomic, strong) CYLabel *titleLBL;//标题
@property (nonatomic, strong) CYLabel *contentLBL;//内容
@property (nonatomic, strong) CYLabel *dateLBL;//日期
@property (nonatomic, strong) CYImageView *dateIMG;//日期图片
@property (nonatomic, strong) CYView *line;//内容与日期分割线
@property (nonatomic, strong) CYView *sectionLine;//cell与cell分割线
@property (nonatomic, strong) CYImageView *rightwardIMG;//右边的箭头
@property (nonatomic, strong) CYImageView *activityIMG;//活动图片
@end
@implementation CYMyNoticeCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        offset_width = 12.5;
        offset_height = 12.5;
    }
    
    return self;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:_title size:AL(PT(30)) weight:UIFontWeightBold];
        _titleLBL.textColor = ColorHex(0xff5b05);
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(offset_width);
            make.top.equalTo(self).with.offset(offset_height);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYLabel *)contentLBL{
    if (!_contentLBL) {
        _contentLBL = [[CYLabel alloc]initWithText:_content size:AL(PT(26)) textcolor:ColorHex(0x656565)];
        [self addSubview:_contentLBL];
        
        [_contentLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(offset_width);
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(offset_height);
            make.size.mas_equalTo([_contentLBL autoSizeWithWidth:(SCREEN_WIDTH - offset_width * 2)]);
        }];
    }
    
    return _contentLBL;
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
        [self addSubview:_line];
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(offset_width);
            make.right.equalTo(self).with.offset(-offset_width);
            make.top.equalTo(_contentLBL.mas_bottom).with.offset(offset_height);
            make.height.mas_equalTo(0.5);
        }];
    }
    
    return _line;
}

- (CYView *)sectionLine{
    if (!_sectionLine) {
        _sectionLine = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
        [self addSubview:_sectionLine];
        
        [_sectionLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(0);
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, AL(PT(10))));
        }];
    }
    return _sectionLine;
}

- (CYImageView *)dateIMG{
    if (!_dateIMG) {
        _dateIMG = [[CYImageView alloc]initWithImageName:@"time" size:CGSizeMake(AL(10), AL(10)) isRadius:YES];
        [self addSubview:_dateIMG];
        
        [_dateIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(offset_width);
            make.top.equalTo(_line.mas_bottom).with.offset(AL(PT(10)));
            make.size.mas_equalTo(_dateIMG.bounds.size);
        }];
    }
    return _dateIMG;
}

- (CYLabel *)dateLBL{
    if (!_dateLBL) {
        _dateLBL = [[CYLabel alloc]initWithText:_date size:AL(PT(22)) textcolor:ColorHex(0xa7a7a7)];
        [self addSubview:_dateLBL];
        
        [_dateLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dateIMG.mas_right).with.offset(AL(3));
            make.centerY.equalTo(_dateIMG);
            make.size.mas_equalTo(_dateLBL.bounds.size);
        }];
    }
    
    return _dateLBL;
}

- (CYImageView *)rightwardIMG{
    if (!_rightwardIMG) {
        _rightwardIMG = [[CYImageView alloc]initWithImageName:@"zuola" size:CGSizeMake(AL(6), AL(10)) isRadius:NO];
        [self addSubview:_rightwardIMG];
        
        WS(self)
        [_rightwardIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(0);
            make.right.equalTo(weakSelf.mas_right).with.offset(- offset_width);
            make.size.mas_equalTo(_rightwardIMG.bounds.size);
        }];
    }
    
    return _rightwardIMG;
}

- (CYImageView *)activityIMG{
    if (!_activityIMG) {
        _activityIMG = [[CYImageView alloc]init];
        _activityIMG.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH - 2 * offset_width, 178 / 754.0 *(SCREEN_WIDTH - 2 * offset_width));
        _activityIMG.loadingView.hidden = NO;
        [self addSubview:_activityIMG];
        
        [_activityIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_contentLBL.mas_bottom).with.offset(offset_width);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 2 * offset_width, 178 / 754.0 *(SCREEN_WIDTH - 2 * offset_width)));
        }];
    }
    
    return _activityIMG;
}

- (void)setTitle:(NSString *)title{
    if (_title != title)
        _title = title;
    self.titleLBL.text = title;
    [self updateTitleLBL];
    
}

- (void)setContent:(NSString *)content{
    if (_content != content)
        _content = content;
    self.contentLBL.text = content;
    [self updateContentLBL];
}


- (void)setDate:(NSString *)date{
    if (_date != date)
        _date = date;
    self.dateLBL.text = date;
    [self updateDateLBL];
    
}

- (void)setImageURL:(NSString *)imageURL{
    if (_imageURL != imageURL) {
        _imageURL = imageURL;
        self.activityIMG.hidden = ([_imageURL length] > 0)?NO:YES;
        [CYHttps loadingWithFile:_imageURL complete:^(id data) {//从URL加载图片
            _activityIMG.image = data;
        }];
    }
}
- (void)setType:(NSString *)type{
    if (_type != type){
        _type = type;
        [self updateSubViewHidden];
    }
}

//更新标题bounds
- (void)updateTitleLBL{
    
    _titleLBL.numberOfLines = 0;
    [_titleLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        if (_titleLBL.bounds.size.height < 0.1)
            make.top.equalTo(self).with.offset(0);
        else
            make.top.equalTo(self).with.offset(offset_height);
        make.size.mas_equalTo([_titleLBL autoSizeWithWidth:(SCREEN_WIDTH - offset_width * 2)]);
    }];
    
}

//更新内容bounds
- (void)updateContentLBL{
    
    _contentLBL.numberOfLines = ([_type isEqualToString:@"activity"])?1:0;
    CGFloat sigle_hight =  [_contentLBL.text sizeWithAttributes:@{NSFontAttributeName:_contentLBL.font}].height;
    [_contentLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        if ([_type isEqualToString:@"activity"])
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - offset_width * 2) / 3.0 * 2.0, sigle_hight));
        else
            make.size.mas_equalTo([_contentLBL autoSizeWithWidth:(SCREEN_WIDTH - offset_width * 2)]);
        if (_contentLBL.bounds.size.height < 0.1)
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(0);
        else
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(offset_height);
    }];
    
}

//更新日期bounds
- (void)updateDateLBL{
    
    [_dateLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_dateLBL.bounds.size);
    }];
    
}

//更新子视图隐藏属性
- (void)updateSubViewHidden{
    /*通知 -->notice   公告 --> public 活动 --> activity*/
    self.titleLBL.hidden = self.contentLBL.hidden = self.sectionLine.hidden = NO;
    self.line.hidden = self.dateIMG.hidden = self.dateLBL.hidden = ([_type isEqualToString:@"activity"])?YES:NO;
    self.activityIMG.hidden = self.activityIMG.loadingView.hidden = self.rightwardIMG.hidden = ([_type isEqualToString:@"activity"])?NO:YES;
    self.rightwardIMG.hidden = ([_type isEqualToString:@"activity"])?NO:YES;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
